﻿using System;

using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Web.Security;
using System.Web.Profile;

using Gift.Model;
using Gift.Model.ENUM;
using Gift.DAL;
using System.Data;


    public partial class admin_login : System.Web.UI.Page
    {

        readonly DALAdminLogin theAdminLogin = new DALAdminLogin();
        readonly DALCompany theCompany = new DALCompany();
        readonly DALDepartment theDepartment = new DALDepartment();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                Session.Clear();
                string sSource = fun.geturl("");

            }
        }

 
        protected void Button1_Click(object sender, ImageClickEventArgs e)
        {
           
            if (Session["checkCode"] == null ||  string.Compare(Session["checkCode"].ToString(), this.txtCheckCode.Text.ToUpper(), true) != 0)
            {
                base.Response.Write("<script language=javascript>alert('验证码错误，请重新输入！');window.location.href='login.aspx';</script>");
            }
            else
            {

                    Session.Clear();
                    string usn = re.replace(this.txtName.Text);
                    string pwd = re.EncodeMD5(this.txtPwd.Text.Trim());
                    bool isLogin = login.lo(usn, pwd);

                    if (isLogin)
                    {
                        LoginAdmin oLoginAdmin = new LoginAdmin();
                        tbAdminLogin o = theAdminLogin.GetModel(usn, pwd);
                        if (o.AdminFlag == 0)
                        {
                            HttpContext.Current.Response.Write("<script>alert('用户账户已停用或未启用，请联系管理员！');</script>");
                            return;
                        }
                        oLoginAdmin.AdminUser = o.AdminUser;
                        oLoginAdmin.AdminUserId = o.AdminId.ToString();
                        oLoginAdmin.AdminTypeId = o.AdminTypeId.ToString();
                        tbCompany oCompany;
                        if (o.DepartmentID == 0)
                        {
                            oCompany = this.theCompany.GetModelByAdminId(int.Parse(oLoginAdmin.AdminUserId));
                            oLoginAdmin.CompanyAdminId = oLoginAdmin.AdminUserId;
                            oLoginAdmin.CompanyId = oCompany.Id.ToString();
                        }
                        else
                        {
                            oLoginAdmin.CompanyId = this.theDepartment.GetModel(o.DepartmentID).CompanyId.ToString();
                             oCompany = this.theCompany.GetModel(int.Parse(oLoginAdmin.CompanyId));
                        }

                        oLoginAdmin.CompanyCode = oCompany.CompanyCode;
                        oLoginAdmin.CompanyLevelId = oCompany.LevelId;
                        oLoginAdmin.GoodsOnSale = oCompany.GoodsOnSale;
                        oLoginAdmin.CardOnSale = oCompany.CardOnSale;
                        new BaseAdmin().Administrator = oLoginAdmin;
                      
                        try
                        {
                            this.Session["inphoneNumber"] = theAdminLogin.GetInphoneNumberByName(usn);
                        }
                        catch { }
                        this.Session.Timeout = 60;
                        Response.Redirect("index.aspx");
                    }
                    else
                    {
                        HttpContext.Current.Response.Write("<script>alert('用户名或密码错误');</script>");
                    }


            }
        }
    }
